package com.itany.corejava.code07_面向对象.bms;

import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2023年04月21日 16:09
 * 银行管理系统主菜单
 */
public class AccountMenu {
    private Scanner sc = new Scanner(System.in);

    // 由于账户菜单必须登录后才能访问
    // 可以限制创建该对象时必须传递Account
    private Account account;

    public AccountMenu(Account account) {
        this.account = account;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public void showMainMenu() {
        System.out.println("************************欢迎进入银行系统主菜单************************");
        System.out.print("请选择服务(1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还款 7.查询贷款金额 8.修改密码 9.注销):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                deposit();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                queryBalance();
                break;
            case 4:
                transfer();
                break;
            case 5:
                providerLoan();
                break;
            case 6:
                payLoan();
                break;
            case 7:
                queryLoan();
                break;
            case 8:
                modifyPassword();
                break;
            case 9:
                logout();
                break;
            default:
                System.out.println("您输入的选项有误,请重新输入!");
                showMainMenu();
        }
    }

    public void deposit() {
        System.out.println("*******************开始存款*******************");
        System.out.print("请输入存款的金额:");
        double money = sc.nextDouble();
        // 调用存款方法
        account.deposit(money);
        System.out.println("提示:存款成功!");
        showMainMenu();
    }

    public void withdraw() {
        System.out.println("*******************开始取款*******************");
        System.out.print("请输入取款的金额:");
        double money = sc.nextDouble();

        // 调用取款方法
        boolean flag = account.withdrawMoney(money);
        if (flag) {
            System.out.println("提示:取款成功!");
        } else {
            System.out.println("错误:余额不足,取款失败!");
        }
        showMainMenu();
    }

    public void queryBalance() {
        System.out.println("*******************查询余额*******************");
        System.out.println("您当前账户余额为:" + account.getBalance() + "元");
        showMainMenu();
    }

    public void transfer() {
        System.out.println("*******************开始转账*******************");
        System.out.print("请输入要转入的目标账户的编号:");
        int id = sc.nextInt();

        // 查询目标账户是否存在
        Account targetAccount = Bank.findById(id);
        // 如果返回值为null,说明对应账户不存在
        if (targetAccount == null) {
            System.out.println("错误:目标账户不存在!");
        } else {
            // 如果目标账户存在,则执行转账功能
            System.out.print("请输入转账的金额:");
            double money = sc.nextDouble();
            // 只有当前账户余额高于转账金额,才可以转账
            if (account.getBalance() >= money) {
                // 当前账户余额减少
                account.setBalance(account.getBalance() - money);
                // 目标账户余额增加
                targetAccount.setBalance(targetAccount.getBalance() + money);
                System.out.println("提示:恭喜您,转账成功,您当前账户余额为:" + account.getBalance());
            } else {
                System.out.println("错误:对不起,转账失败,您的账户余额不足,您当前账户余额为:" + account.getBalance());
            }
        }
        showMainMenu();
    }


    public void providerLoan() {
        System.out.println("*******************开始贷款*******************");
        // 只有账户为贷款账户,才能进行贷款
        Loanable loan = new LoanSavingAccount();
        Loanable loan2 = new LoanCreditAccount();
        // 所有的贷款账户都实现了Loanable接口
        // 因此,可以直接通过接口来进行判断
        // 能转换成功对应接口的类一定是该接口对应的两个实现类之一
        if (account instanceof Loanable) {
            System.out.print("请输入贷款金额:");
            double money = sc.nextDouble();
            // 贷款功能只有Loanable接口对应的实现类提供
            // 其实现类有多种,因此可以使用实现类的对象指向接口的引用
            // 将当前账户强制转换成Loanable接口类型
            Loanable loanable = (Loanable) account;
            // 执行贷款方法
            loanable.providerLoan(money);
            System.out.println("提示:恭喜您,贷款成功,您当前贷款总额为:" + loanable.queryLoan());
        } else {
            System.out.println("错误:对不起,您的账户不是贷款账户,无法操作贷款功能");
        }
        showMainMenu();
    }

    public void payLoan() {
        System.out.println("*******************开始还贷*******************");
        // 只有贷款账户可以执行贷款方法
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.print("请输入您需要还贷的金额:");
            double money = sc.nextDouble();
            boolean flag = loanable.payLoan(money);
            if (flag) {
                System.out.println("提示:恭喜您,还贷成功,当前账户余额:" + account.getBalance() + ",当前贷款总额:" + loanable.queryLoan());
            } else {
                System.out.println("错误:对不起,您的账户余额不足,还贷失败! 您的账户余额为:" + account.getBalance());
            }
        } else {
            System.out.println("错误:对不起,您的账户不是贷款账户,无法操作贷款功能");
        }
        showMainMenu();
    }

    public void queryLoan() {
        System.out.println("*******************查询贷款总额*******************");
        // 只有贷款账户可以执行贷款方法
        if (account instanceof Loanable) {
            Loanable loanable = (Loanable) account;
            System.out.println("提示:您当前贷款总额为:" + loanable.queryLoan());
        } else {
            System.out.println("错误:对不起,您的账户不是贷款账户,无法操作贷款功能");
        }
        showMainMenu();
    }

    public void modifyPassword() {
        System.out.println("*******************修改密码*******************");
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        if (oldPassword.equals(account.getPassword())) {
            // 如果原密码正确,则执行修改密码操作
            System.out.print("请输入新的密码:");
            String newPassword = sc.next();
            System.out.print("请再次输入新的密码:");
            String newPassword2 = sc.next();
            if (newPassword.equals(newPassword2)) {
                // 如果两次输入的密码一致,则进行修改
                account.setPassword(newPassword);
                System.out.println("提示:密码修改成功,请重新登录!");
                new Start().showMenu();
            } else {
                System.out.println("错误:两次输入的密码不一致,请重新输入");
            }
        } else {
            System.out.println("错误:您输入的原密码不正确,请重新输入!");
        }
        showMainMenu();
    }

    public void logout() {
        System.out.println("提示:注销成功!");
        new Start().login();
    }
}
